package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.SpecVO;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationOptionService;
import com.dongdongshop.service.TbSpecificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("specificationController")
public class SpecificationController {
        @Reference
        private TbSpecificationService tbSpecificationService;
        @Reference
        private TbSpecificationOptionService tbSpecificationOptionService;
    @RequestMapping("specification")
    public String toSpecification(){
    return "admin/specification";
    }
    //查询
    @RequestMapping("toFindAll")
    @ResponseBody
    public Result toFndAll(){
         List<TbSpecification> specificationsList = tbSpecificationService.selectAll();
         return new Result(1000,"查询成功",specificationsList);
    }
    //增加
    @RequestMapping("toAddSpec")
    @ResponseBody
    public Result toAddSpec(TbSpecification tbSpecification,String optionList){
        List<TbSpecificationOption> TbSpecificationOption = JSONObject.parseArray(optionList, TbSpecificationOption.class);
        System.out.println(TbSpecificationOption);
        try {
            tbSpecificationService.addSpecBatch(tbSpecification,TbSpecificationOption);
        }catch (Exception e){
            return new Result(100001,"增加失败",null);
        }
        return new Result(10000,"增加成功",null);
    }
    //回显
    @RequestMapping("echoSpec/{id}")
    @ResponseBody
    public Result toEchoSpec(@PathVariable("id")Integer id){
         SpecVO specVO = tbSpecificationService.selectById(id);
         return new Result(10000,"回显成功",specVO);
    }
    //修改
    @RequestMapping("updatespec2")
    @ResponseBody
    public Result updateSpec2(TbSpecification tbSpecification,String optionListJson){
        //将字符串转换为集合
        List<TbSpecificationOption> tbSpecificationOptions = JSONObject.parseArray(optionListJson, TbSpecificationOption.class);
        //修改
        tbSpecificationService.updateSpec(tbSpecification,tbSpecificationOptions);
//            tbSpecificationService.updateSpec(tbSpecification,tbSpecificationOptions);
        return new Result(10000,"修改成功",null);
    }
    //删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result toDeleteBatch(Long [] ids){
        tbSpecificationService.selectBatch(ids);
        return new Result(10000,"删除成功",null);
    }
}
